//
//  GuideView.m
//  爱鲜蜂Guide
//
//  Created by 王锐 on 17/1/3.
//  Copyright © 2017年 王锐. All rights reserved.
//

#import "GuideView.h"
#import "Masonry.h"
#import "OpMainTabBarController.h"
@interface GuideView ()<UIScrollViewDelegate>
@property(nonatomic,weak)UIScrollView* scrollView;
@property(nonatomic,weak)UIPageControl* pageControl;

@end

@implementation GuideView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        //1.添加scrollView
        [self makeScrollView];
        //2.创建分页指示器
        UIPageControl* pageControl = [[UIPageControl alloc] init];
        self.pageControl = pageControl;
        //设置分页指示器当前页的颜色
        pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        //设置其他页颜色
        pageControl.pageIndicatorTintColor = [UIColor colorWithRed:252 green:206 blue:47 alpha:0];
        //设置默认当前页
        pageControl.currentPage = 0;
        //把分页指示器添加到新特性view
        [self addSubview:pageControl];
        [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.mas_bottom).offset(-20);
            make.centerX.equalTo(self.mas_centerX);
        }];
        //让小点不能点击
        pageControl.enabled = NO;
        
    }
    return self;
}





#pragma mark - 添加scrollView
- (void)makeScrollView
{
    //1.创建scrollView
    UIScrollView* scrollView = [[UIScrollView alloc] init];
    self.scrollView = scrollView;
    //2.把scrollView添加到guideView上
    [self addSubview:scrollView];
    //3.给scrollView添加约束
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        //scrollView和父控件一样大,距离父控件四周0间距
        [make edges];
    }];
    //4.设置是否分页
    scrollView.pagingEnabled = YES;
    //不要弹簧效果
    scrollView.bounces = NO;
    //隐藏滚动条
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    //5.给scrollView设置代理
    scrollView.delegate = self;
}




#pragma mark - 重写属性的set方法拦截数据传递过程
- (void)setImageName:(NSArray *)imageName
{
    _imageName = imageName;
    
    //根据图片个数来for循环创建及添加scrollView中的内容
    for (NSInteger i = 0; i < imageName.count; i++)
    {
        //1.创建新特性中的图片
        UIImageView* imageView = [[UIImageView alloc] init];
        //2.获取图片路径
        NSString* path = [[NSBundle mainBundle]pathForResource:imageName[i] ofType:nil];
        //3.根据指定路径加载图片
        imageView.image = [UIImage imageWithContentsOfFile:path];
        
        //4.设置图片的frame
        imageView.frame = CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height);
        //5.给scrollView中添加图片
        [self.scrollView addSubview:imageView];
        
        //6.添加加载更多的按钮
        [self makeLoadMoreButton:imageView];
        
        //7.把imageView的交互打开
        imageView.userInteractionEnabled = YES;
    }
    
    //8.设置scrollView滚动视图
    self.scrollView.contentSize = CGSizeMake((imageName.count + 1) * self.bounds.size.width, 0);
    
    //9.设置总页数
    self.pageControl.numberOfPages = imageName.count;
}






#pragma mark - 给imageView上添加加载更多的按钮
- (void)makeLoadMoreButton:(UIImageView *)imageView
{
    static int i = 0;
    if(i == 3)
    {
        //1.创建按钮
        UIButton* btn = [[UIButton alloc] init];
        //2.设置内容
        [btn setImage:[UIImage imageNamed:@"icon_next"] forState:UIControlStateNormal];
        //3.添加到相应的父控件
        [imageView addSubview:btn];
        //4.设置约束  添加约束前一定要保存控件已经被添加
        [btn sizeToFit];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(imageView.mas_centerX);
            make.bottom.equalTo(self.pageControl.mas_top).offset(-20);
        }];
        // 5.给按钮添加监听方法
        [btn addTarget:self action:@selector(loadMoreBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    i++;
#warning mark - 按钮的位置一定要参照它自己的父控件

}

#pragma mark - 加载更多按钮点击方法
- (void)loadMoreBtnClick:(UIButton *)btn
{
    //1.把加载更多按钮隐藏
    btn.hidden = YES;
    //2.动画方式让当前所有显示的这张图片放大,并且慢慢透明
    [UIView animateWithDuration:2.0 animations:^{
        //3.让按钮所在的父控件变大
        btn.superview.transform = CGAffineTransformMakeScale(2, 2);
        //4.让按钮所在的父控件透明
        btn.superview.alpha = 0;
    }
     completion:^(BOOL finished) {
         //5.把新特性界面移除
         [self removeFromSuperview];
       OpMainTabBarController * headPage=[[OpMainTabBarController alloc]init];
         [[UIApplication sharedApplication].keyWindow setRootViewController:headPage];
     }];
}

#pragma mark - 只要滚动就会调用此方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //计算当前滚动页数,多家0.499是为了让新的一页只要出来一半多一点就显示到下一页
    NSInteger page = scrollView.contentOffset.x / scrollView.bounds.size.width + 0.49999;
    //设置当前页数
    self.pageControl.currentPage = page;
    //如果主界面显示一大半时就把分页指示器隐藏
    if (page == self.imageName.count)
    {
        self.pageControl.hidden = YES;
    }
    else
    {
        self.pageControl.hidden = NO;
    }
    //记录当前滚动到第几页
    scrollView.tag = page;
}

//降速完成就会调用一次此方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //如果已经完全显示到主界面了,应该把新特性界面移除
    if (scrollView.tag == self.imageName.count)
    {
        //把新特性移除
        [self removeFromSuperview];
    }
}








@end
